using Unity.Burst;
using Unity.Entities;
using Unity.Transforms;

namespace HelloCube.EnableableComponents
{
    public partial struct RotationSystem : ISystem
    {
        const float mInterval = 0.85f;
        float mTimer;

        [BurstCompile]
        public void OnCreate(ref SystemState state)
        {
            mTimer = mInterval;
            state.RequireForUpdate<Execute.EnableableComponents>();
        }

        [BurstCompile]
        public void OnUpdate(ref SystemState state)
        {
            float deltaTime = SystemAPI.Time.DeltaTime;
            mTimer -= deltaTime;

            if (mTimer < 0)
            {
                //切换状态
                foreach (var rotationSpeedEnabled in SystemAPI.Query<EnabledRefRW<RotateSpeed2>>().WithOptions(EntityQueryOptions.IgnoreComponentEnabledState))
                {
                    rotationSpeedEnabled.ValueRW = !rotationSpeedEnabled.ValueRO;
                }

                mTimer = mInterval;
            }

            foreach (var (transform, rotationSpeed) in SystemAPI.Query<RefRW<LocalTransform>, RefRO<RotateSpeed2>>())
            {
                transform.ValueRW = transform.ValueRO.RotateY(rotationSpeed.ValueRO.RadiansPerSecond * deltaTime);
            }
        }
    }
}